<!--
 * @Author: zhangzao
 * @Date: 2024-12-12 13:42:58
 * @LastEditors: zhangzao
 * @LastEditTime: 2024-12-12 14:42:02
 * @Description: 请填写简介
 * @FilePath: /tobeno1/src/views/activity-content/chat/index.vue
-->
<template>
  <div class="app-container">
    <!-- 搜索区域 -->
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      size="small"
    >
      <el-form-item label="选手" prop="playerId">
        <el-select
          v-model="queryParams.playerId"
          placeholder="请选择选手"
          clearable
          size="small"
        >
          <el-option
            v-for="item in playerOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 操作按钮区域 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-upload
          :action="uploadUrl"
          :show-file-list="false"
          :before-upload="beforeUpload"
          :on-success="handleUploadSuccess"
          :on-error="handleUploadError"
        >
          <el-button type="primary" icon="el-icon-upload" size="mini"
            >批量创建</el-button
          >
        </el-upload>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格区域 -->
    <el-table v-loading="loading" :data="chatList">
      <el-table-column
        prop="id"
        label="ID"
        width="80"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="playerName"
        label="选手"
        width="120"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="roomName"
        label="聊天室名称"
        align="center"
      ></el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="180"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" align="center" width="120" fixed="right">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.row)">修改</el-button>
          <el-button type="text" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 编辑弹窗 -->
    <edit-dialog
      :visible.sync="open"
      :title="title"
      :data="form"
      :player-options="playerOptions"
      @submit="handleSubmit"
    />
  </div>
</template>

<script>
import { listPlayer } from "@/api/player";
import {
  listChatRoom,
  addChatRoom,
  editChatRoom,
  deleteChatRoom,
} from "@/api/chatroom";
import EditDialog from "./components/edit.vue";

export default {
  name: "ChatRoom",
  components: {
    EditDialog,
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 聊天室列表
      chatList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        playerId: undefined,
        playerName: undefined,
      },
      // 表单参数
      form: {
        id: undefined,
        playerId: undefined,
        playerName: undefined,
        roomName: ""
      },
      // 选手选项
      playerOptions: [],
      // 上传地址
      uploadUrl: process.env.VUE_APP_BASE_API + "/chat/import",
    };
  },
  created() {
    this.getList();
    this.getPlayerOptions();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listChatRoom(this.queryParams)
        .then((response) => {
          this.chatList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    /** 获取选手选项 */
    getPlayerOptions() {
      listPlayer().then((response) => {
        this.playerOptions = response.rows.map((item) => ({
          label: item.name,
          value: item.id,
        }));
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        playerId: undefined,
        playerName: undefined,
        roomName: ""
      };
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增聊天室";
    },
    /** 编辑按钮操作 */
    handleEdit(row) {
      this.reset();
      this.form = { ...row };
      this.open = true;
      this.title = "编辑聊天室";
    },
    /** 处理弹窗提交 */
    handleSubmit(formData) {
      // 从playerOptions中获取对应的playerName
      const player = this.playerOptions.find(
        (item) => item.value === formData.playerId
      );
      formData.playerName = player ? player.label : "";

      const submitMethod = formData.id ? editChatRoom : addChatRoom;
      submitMethod(formData).then(() => {
        this.$modal.msgSuccess("保存成功");
        this.open = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm("是否确认删除该聊天室？")
        .then(() => {
          deleteChatRoom([row.id]).then(() => {
            this.$modal.msgSuccess("删除成功");
            this.getList();
          });
        })
        .catch(() => {});
    },
    /** 上传文件前的校验 */
    beforeUpload(file) {
      const isExcel =
        file.type ===
          "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ||
        file.type === "application/vnd.ms-excel";
      if (!isExcel) {
        this.$message.error("只能上传 Excel 文件!");
        return false;
      }
      return true;
    },
    /** 上传成功处理 */
    handleUploadSuccess(response) {
      this.$modal.msgSuccess("导入成功");
      this.getList();
    },
    /** 上传失败处理 */
    handleUploadError() {
      this.$modal.msgError("导入失败");
    },
  },
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}
.mb8 {
  margin-bottom: 8px;
}
</style>
